//
//  PicTableViewCell.swift
//  Pods
//
//  Created by eme on 2017/4/7.
//
//

import UIKit

class PicTableViewCell: UITableViewCell {
    
    @IBOutlet weak var pic_CollectionView: UICollectionView!
    
    var imagekeys = [String](){
        didSet{
            self.pic_CollectionView.reloadData()
        }
    }
    @IBOutlet weak var collection_height: NSLayoutConstraint!
    let MyLayout = UICollectionViewFlowLayout()

    override func awakeFromNib() {
        
        super.awakeFromNib()
        let screenBounds:CGFloat = UIScreen.main.bounds.width
        
        MyLayout.itemSize = CGSize(width:(screenBounds - 75)/4, height:(screenBounds - 75)/4)
        MyLayout.scrollDirection = UICollectionViewScrollDirection.vertical
        MyLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5)
        pic_CollectionView.collectionViewLayout = MyLayout
        pic_CollectionView.register(AddNewPicCollectionViewCell.self, forCellWithReuseIdentifier: "cameraCell")
        pic_CollectionView.register(MessageImageCollectionViewCell.self, forCellWithReuseIdentifier: "imageCell")
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension EvaluationVc:UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imageArr[collectionView.tag].count + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        switch indexPath.row {
            
        case self.imageArr[collectionView.tag].count:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraCell", for: indexPath) as! AddNewPicCollectionViewCell
            
            return cell
        default:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! MessageImageCollectionViewCell
            
            
            cell.deleteButton.imageView?.tag = collectionView.tag
            
            cell.deleteButton.tag = indexPath.row
            
            cell.deleteButton.addTarget(self, action: #selector(EvaluationVc.deleteImage(_:)), for: .touchUpInside)
            if self.imageArr[collectionView.tag].count != 0{
                cell.customImageView.setUrlImage(self.imageArr[collectionView.tag][indexPath.row])
            }
            
            return cell
        }
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.pic_Section = collectionView.tag
        switch indexPath.row {
        case self.imageArr[collectionView.tag].count:
            if self.imageArr.count == 0{
                selectIcon()
            }else{
                if self.imageArr[collectionView.tag].count < 3{
                    selectIcon()
                }else{
                    self.view.toast("最多上传3张图片")
//                    self.view.makeEmeToast("")
                }
            }
        default:
            break
        }
    }
    //-----------------------------------
    ///改变头像
    func selectIcon()
    {
        let userIconAlert = UIAlertController(title: "请选择操作", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        
        let chooseFromPhotoAlbum = UIAlertAction.init(title: "从相册选择", style: UIAlertActionStyle.default, handler: { UIAlertAction in
            self.ChooseFromPhotoAlbum()
        })
        
        let chooseFromCamera = UIAlertAction(title: "拍照", style: UIAlertActionStyle.default,handler:{ UIAlertAction in
            self.ChooseFromCamera()
        })
        
        let canelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel,handler: nil)
        
        userIconAlert.addAction(canelAction)
        userIconAlert.addAction(chooseFromCamera)
        userIconAlert.addAction(chooseFromPhotoAlbum)
        
        self.present(userIconAlert, animated: true, completion: nil)
        
    }
    //MARK:-
    //MARK:从相册选取图片
    func ChooseFromPhotoAlbum() -> Void{
        let imagePicker = UIImagePickerController()
        //设置代理
        imagePicker.delegate = self
        //允许编辑
        imagePicker.allowsEditing = true
        //设置图片源
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        //模态弹出IamgePickerView
        self.present(imagePicker, animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
    //MARK:-
    //MARK:拍照
    func ChooseFromCamera() -> Void{
        let imagePicker = UIImagePickerController()
        //设置代理
        imagePicker.delegate = self
        //允许编辑
        imagePicker.allowsEditing=true
        //设置图片源
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        //模态弹出IamgePickerView
        self.present(imagePicker, animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //获得编辑后的图片
        if let image = (info as NSDictionary).object(forKey: UIImagePickerControllerEditedImage) as? UIImage  {
            self.qiniuVm?.uploadImageSubject.onNext(image)
        }
        picker.dismiss(animated: true, completion: nil)
    }
}
